package com.benyq.networkprogramme.ftp

import android.util.Log
import org.apache.commons.net.ftp.FTPClient
import org.apache.commons.net.ftp.FTPClientConfig
import org.apache.commons.net.ftp.FTPFile
import org.apache.commons.net.ftp.FTPReply
import java.io.File
import java.io.FileInputStream
import java.io.FileOutputStream
import java.io.IOException
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors

/**
 * @author benyq
 * @date 18-9-26 21:16
 * @E-mail： 1520063035@qq.com
 * @description
 */
object FtpHelper {

    private val hostName = "192.168.0.104"
    private val userName = "benyq"
    private val password = "yezijian520"
    private val defaultPort = 21

    private val ftpClient by lazy { FTPClient() }
    private var executorService : ExecutorService = Executors.newSingleThreadExecutor()

    init {
        ftpClient.defaultPort = defaultPort
    }

    fun openConnect(){
        var reply = 1111
        ftpClient.controlEncoding = "UTF-8"
        ftpClient.connect(hostName)
        reply = ftpClient.replyCode

        if (!FTPReply.isPositiveCompletion(reply)) {
            // 断开连接
            ftpClient.disconnect()
            throw IOException("connect fail: $reply")
        }
        // 登录到服务器
        ftpClient.login(userName, password)
        // 获取响应值，判断登陆结果
        reply = ftpClient.replyCode
        if (!FTPReply.isPositiveCompletion(reply)) {
            // 断开连接
            ftpClient.disconnect()
            throw IOException("connect fail: $reply")
        } else {
            // 获取登录信息
            val config = FTPClientConfig(ftpClient.systemType.split(" ")[0])
            config.serverLanguageCode = "zh"
            ftpClient.configure(config)
            // 使用被动模式设为默认
//            ftpClient.enterLocalPassiveMode();
//            ftpClient.enterLocalActiveMode();
//            ftpClient.enterRemotePassiveMode();
//            // 二进制文件支持
            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE)
        }

    }

    fun closeConnect(){
        ftpClient.logout()
        ftpClient.disconnect()
    }

    fun isConnect(): Boolean{
        return ftpClient.isConnected
    }

    fun listFiles(remotePath: String): List<FTPFile>{
        //val files = mutableListOf<FTPFile>()
        val b = ftpClient.changeWorkingDirectory(remotePath)
        val files = ftpClient.listFiles()
        Log.e("susan", "${b}ftp${files.size}")
        return listOf(*files)
    }


    fun uploadFile(localFile: File): Boolean{
//        val ins = FileInputStream(localFile)
//        val flag = ftpClient.storeFile(localFile.name, ins)
//        ins.close()
        var status: Boolean = false
        try {
            val srcFileStream = FileInputStream(localFile)
            status = ftpClient.storeFile(localFile.name, srcFileStream)
            srcFileStream.close()
            return status
        } catch (e: Exception) {
            e.printStackTrace()
            Log.d("susan", "upload failed: " + e.localizedMessage)
        }

        return status
    }

    fun downFile(localFile: File, ftpFile: FTPFile): Boolean{
        val out = FileOutputStream(localFile)
        val flag = ftpClient.retrieveFile(ftpFile.name, out)
        out.close()
        return flag
    }


}